/// for loop and while study
fn test_for() {
    for i in 1..5 {  // [)
        println!("{}", i);
    }
    // 1  2 3 4
}

// while
fn test_while() {
    let mut i = 0;
    while i < 5 {
        println!("{}", i);
        i += 1;
    }
    // 0 1 2 3 4
}

//loop
fn test_loop() {
    let mut i = 0;
    loop {
        println!("{}", i);
        i += 1;
        if i == 5 {
            break;
        }
    }
    // 0 1 2 3 4
}

//for and iterators

// for iter:这将在每次迭代中借用集合中的每个元素。从而使集合保持不变，并且在循环之后可以重用。
pub fn for_iter() {
    let names = vec!["Bob", "Frank", "Ferris"];
    for name in names.iter() {
        // is equal to : if name == Ferris {} else {}
        match name {
            &"Ferris" => println!("There is a rustacean among us!"),
            _ => println!("Hello {}", name),
        }
    }

    println!("names: {:?}", names);
}

// for into_iter：这将消耗集合，以便在每次迭代中提供准确的数据。一旦集合被消耗，它就不再可用，因为它已经在循环中“移动”了。
pub fn for_into_iter() {
    let names = vec!["Bob", "Frank", "Ferris"];
    for name in names.into_iter() {
        match name {
            "Ferris" => println!("There is a rustacean among us!"),
            _ => println!("Hello {}", name),
        }
    }
    // println!("{:?}", names);
    // 使用了 `into_iter()` 方法来获取一个消耗式迭代器，它将所有权转移给了循环体内部的 `name` 变量。
    //因此，在循环结束后，`names` 变量已经被 move，不能再次访问
}

// for iter_mut
pub fn for_iter_mut() {
    let mut names = vec!["Bob", "Frank", "Ferris"];

    for name in names.iter_mut() {
        *name = match name {
            &mut "Ferris" => "There is a rustacean among us!",
            _ => "Hello",
        }
    }
    println!("names: {:?}", names);
}

